package com.java.comprehensive.arithmetic.leecode.easy;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;

public class LeetCode_225 {

    static class MyStack {

        private Queue<Integer> queue = new LinkedBlockingDeque<>();

        public MyStack() {

        }

        public void push(int x) {
            queue.add(x);
        }

        public int pop() {
            if (!queue.isEmpty()) {
                for (int i = 0; i < queue.size() - 1; i++) {
                    Integer poll = queue.poll();
                    queue.add(poll);
                }
                return queue.poll();
            }
            return 0;
        }

        public int top() {
            if (!queue.isEmpty()) {
                for (int i = 0; i < queue.size() - 1; i++) {
                    Integer poll = queue.poll();
                    queue.add(poll);
                }
                Integer peek = queue.poll();
                queue.add(peek);
                return peek;
            }
            return 0;
        }

        public boolean empty() {
            return queue.isEmpty();
        }
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        System.out.println(myStack.top());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
    }
}
